import os
import re
from collections import Counter
import PyPDF2
import csv


def extract_words_from_pdf(file_path):
    # 使用PyPDF2打开PDF文件
    pdf_file = open(file_path, 'rb')
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    text = ""
    
    # 提取PDF中的所有文本
    for page_num in range(len(pdf_reader.pages)):
        page = pdf_reader.pages[page_num]
        text += page.extract_text()
    
    # 关闭PDF文件
    pdf_file.close()
    
    # 使用正则表达式提取英文单词
    words = re.findall(r'\b[a-zA-Z]+\b', text.lower())  # 转换为小写以统一处理
    print(words)
    return words

def count_word_frequency(words):
    word_counts = Counter(words)
    return word_counts

def main(pdf_folder, output_csv):
    all_words = []
    
    # 遍历PDF文件夹中的所有PDF文件
    for file_name in os.listdir(pdf_folder):
        if file_name.endswith('.pdf'):
            file_path = os.path.join(pdf_folder, file_name)
            words = extract_words_from_pdf(file_path)
            all_words.extend(words)
    
    # 进行词频统计
    word_frequencies = count_word_frequency(all_words)
    
    # 打印词频统计结果
    with open(output_csv, 'w', newline='', encoding='utf-8-sig') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(["Word", "Frequency"])  # 写入表头
            for word, freq in word_frequencies.most_common():
                writer.writerow([word, freq])

if __name__ == "__main__":
    pdf_folder = 'opdf'  # 替换为你的PDF文件夹路径
    output_csv = 'output.csv'
    main(pdf_folder,output_csv)
